# ============================================================================
# Table 4: Number of respondents by condition
# ============================================================================

tab_4 <- table(Policy = ifelse(test = data$police == 1, yes = "Race-conscious policy", no = "Income-conscious policy"),
               Name   = ifelse(test = data$deandre == 1, yes = "Black Name", no = "White Name"))

# Extract counts
white_race <- tab_4["Race-conscious policy", "White Name"]
black_race <- tab_4["Race-conscious policy", "Black Name"]
white_income <- tab_4["Income-conscious policy", "White Name"]
black_income <- tab_4["Income-conscious policy", "Black Name"]
total_race <- white_race + black_race
total_income <- white_income + black_income
total_white <- white_race + white_income
total_black <- black_race + black_income
grand_total <- total_white + total_black

table_4 <- sprintf(fmt = "\\caption{Number of respondents by experimental condition (source name x policy type)}
\\centering
\\begin{tabular}{llll}
\\hline
~ & White Name & Black Name & Total \\\\ \\hline
Race-conscious policy & %d & %d & %d \\\\
Income-conscious policy & %d & %d & %d \\\\ \\hline
Total & %d & %d & %s \\\\ \\hline
\\end{tabular}",
                   white_race,
                   black_race,
                   total_race,
                   white_income,
                   black_income,
                   total_income,
                   total_white,
                   total_black,
                   format_int(grand_total))

writeLines(text = table_4, con = "code_and_output/tables/table_4.tex")

rm(list = setdiff(ls(), c("data", "fmt", "format_int", "n_sims")))
gc()